﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QLNhaThuoc.BL;
using QLNhaThuoc.DTO;
using QLNhaThuoc.MyClass;

namespace QLNhaThuoc
{
    public partial class Form_DMHoaDon : Form
    {
        public Form_DMHoaDon()
        {
            InitializeComponent();
        }


        HoaDonControl hoaDonCtrl;
        BindingSource bs;
        Form_HoaDon frm;
        private HoaDon[] dsHoaDon;
        private void Form_DMHoaDon_Load(object sender, EventArgs e)
        {
            hoaDonCtrl = new HoaDonControl();
            bs = new BindingSource();
            XuatListHoaDon();
            tìmToolStripMenuItem.Enabled = false;
        }

        void frm_handle_formClosed()
        {
            XuatListHoaDon();
        }

        private void XuatListHoaDon()
        {
            dsHoaDon = hoaDonCtrl.LayListHoaDon();
            bs.DataSource = dsHoaDon;
            dgvHoaDon.DataSource = null;
            dgvHoaDon.AutoGenerateColumns = false;
            dgvHoaDon.DataSource = bs;
        }

        private void btThem_Click(object sender, EventArgs e)
        {
            frm = new Form_HoaDon();
            frm.handle_formClosed += new Form_HoaDon.formClosed(frm_handle_formClosed);
            frm.MdiParent = this.MdiParent;
            frm.hoaDonCtrl = hoaDonCtrl;
            frm.isAdding = true;
            frm.Show();
        }

        private void btXoa_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Bạn có thật sự muốn xóa không ?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.No)
                return;

            HoaDon info = (HoaDon)bs.Current;
            hoaDonCtrl.XoaHD(info);
            XuatListHoaDon();
        }

        private void btXem_Click(object sender, EventArgs e)
        {
            if (bs.Count > 0)
            {
                HoaDon hoaDonInfo = (HoaDon) bs.Current;
                frm = new Form_HoaDon();
                frm.handle_formClosed+=new Form_HoaDon.formClosed(frm_handle_formClosed);
                frm.MdiParent = this.MdiParent;
                frm.isViewing = true;
                frm.hoaDonInfo = hoaDonInfo;
                frm.hoaDonCtrl = hoaDonCtrl;
                frm.Show();
            }
        }

        private void mãHóaĐơnToolStripMenuItem_Click(object sender, EventArgs e)
        {
            CheckMenuItem(sender);
        }

        private void mãNhânViênToolStripMenuItem_Click(object sender, EventArgs e)
        {
            CheckMenuItem(sender);
        }

        private void ngàyXuấtToolStripMenuItem_Click(object sender, EventArgs e)
        {
            CheckMenuItem(sender);
        }

        private void tênNgườiMuaToolStripMenuItem_Click(object sender, EventArgs e)
        {
            CheckMenuItem(sender);
        }

        private void CheckMenuItem(object sender)
        {
            foreach (ToolStripMenuItem item in chọnTiêuChíTìmKiếmToolStripMenuItem.DropDownItems)
            {
                item.Checked = false;
            }
            ((ToolStripMenuItem)sender).Checked = true;
            tìmToolStripMenuItem.Enabled = true;
        }

        private void tìmToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (tstbTim.Text != "")
            {
                string value = tstbTim.Text;
                SearchStrategy strategy = null;
                string expr = "";

                if (mãHóaĐơnToolStripMenuItem.Checked == true)
                {
                    strategy = new SearchStringStrategy();
                    strategy.SetParameter("MaHD", value);
                }
                else if (mãNhânViênToolStripMenuItem.Checked == true)
                {
                    strategy = new SearchStringStrategy();
                    strategy.SetParameter("MaNV", value);
                }
                else if (ngàyXuấtToolStripMenuItem.Checked == true)
                {
                    try
                    {
                        strategy = new SearchDateStrategy();
                        strategy.SetParameter("NgayXuat", value);
                    }
                    catch
                    {
                        MessageBox.Show("Bạn phải nhập ngày theo dạng dd/MM/yyyy");
                    }                                     
                }
                else if (tênNgườiMuaToolStripMenuItem.Checked == true)
                {
                    strategy = new SearchStringStrategy();
                    strategy.SetParameter("TenNguoiMua", value);
                }

                if(strategy != null)
                {
                    expr = strategy.GetSearchExpression();
                    bs.DataSource = hoaDonCtrl.TimKiem(expr);
                    dgvHoaDon.DataSource = bs;
                }                
            }
            else
            {
                XuatListHoaDon();
            }
        }
    }
}
